package com.rc.evcharger.mq;

import com.alibaba.fastjson.JSON;
import com.rc.evcharger.action.StdBizAction;
import com.rc.evcharger.dto.msg.TerminalMessage;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.listener.api.ChannelAwareMessageListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 启停充电（桩）指令监听器
 * Created by sven on 2019/3/4
 */
@Component("command8EListener")
public class Command8EListener implements ChannelAwareMessageListener {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private StdBizAction stdBizAction;

    @Override
    public void onMessage(Message message, Channel channel) throws Exception {
        String msg = new String(message.getBody(), "UTF-8");
        logger.info("command8EListener 收到了:{},channel:{}", msg, channel.getConnection());

        TerminalMessage terminalMessage;
        try {
            terminalMessage = JSON.parseObject(msg, TerminalMessage.class);

            //启动和停止是同一指令，需要在MQ监听器进行区别
            stdBizAction.readElecControl(terminalMessage);
        } catch (Exception e) {
            logger.warn("command8EListener 处理失败", e);
            return;
        }
    }
}
